package com.yomahub.liteflow.example.xstream;

import com.sun.org.apache.xerces.internal.xs.XSTerm;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.persistence.FilePersistenceStrategy;
import com.thoughtworks.xstream.persistence.PersistenceStrategy;
import com.thoughtworks.xstream.persistence.XmlArrayList;
import com.thoughtworks.xstream.security.AnyTypePermission;
import com.yomahub.liteflow.example.xstream.converter.AuthorConverter;
import com.yomahub.liteflow.example.xstream.converter.PersonConverter;
import com.yomahub.liteflow.example.xstream.converter.PersonSingleValueConverter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: liteflow-example
 * @description:
 * @author: yyyyjinying
 * @create: 2023-09-25 16:54
 * 可以使用类别名更改标记名
 *
 * 可以使用字段别名更改标记名称
 *
 * 可以使用包别名更改标记名称
 *
 * 如果字段类型由SingleValueConverter处理，则可以将字段写入属性
 **/
public class Client {

    public static void xmlToObject() {
        String xml = "<user>\n" +
                "  <id>1</id>\n" +
                "  <name>xx</name>\n" +
                "  <password>888</password>\n" +
                "</user>";

        // 创建XStream
        XStream xStream = new XStream(new DomDriver("UTF-8"));

//        放开权限
        xStream.addPermission(AnyTypePermission.ANY);

        // 起别名
//        xStream.alias("user", MyUser.class);
//        xStream.autodetectAnnotations(true);
        // 对象转化时需要手动扫描注解(自动不生效)
        xStream.processAnnotations(MyUser.class);
        MyUser user = (MyUser) xStream.fromXML(xml);
        System.out.println(user);
    }

    public static void test(){
//        / prepares the file strategy to directory /tmp
        PersistenceStrategy strategy = new FilePersistenceStrategy(new File("/tmp"));
        // creates the list:
        List list = new XmlArrayList(strategy);


        // adds four authors
        list.add(new Author("joe walnes"));
        list.add(new Author("joerg schaible"));
        list.add(new Author("mauro talevi"));
        list.add(new Author("guilherme silveira"));

        // adding an extra author
        Author mistake = new Author("mama");
        list.add(mistake);
    }

    public static void demo6(){
        Person person = new Person();
        person.setName("Guilherme");

        XStream xStream = new XStream(new DomDriver());
//        xStream.registerConverter(new PersonConverter());
        xStream.registerConverter(new PersonSingleValueConverter());
        xStream.alias("person", Person.class);
        String s = xStream.toXML(person);
        System.out.println(s);
    }


    public static void demo5(){
        XStream xStream = new XStream();
//        xStream.processAnnotations(RendezvousMessage.class);
//        RendezvousMessage message = new RendezvousMessage(12, true, "a", "b", "c");
//        xStream.processAnnotations(RendezvousMessageTwo.class);
        xStream.autodetectAnnotations(true);
        RendezvousMessageTwo message = new RendezvousMessageTwo(12, true, "abc");
        String s = xStream.toXML(message);
        System.out.println(s);
    }

    public static void demo4() {
        Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
        teamBlog.add(new Entry("first","My first blog entry."));
        teamBlog.add(new Entry("tutorial",
                "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));

        XStream xstream = new XStream();
        // 修改包的别名 将2改为1
        xstream.aliasPackage("org.thoughtworks", "com.yomahub.liteflow.example");
        System.out.println(xstream.toXML(teamBlog));


    }
    public static void demo3() {
        Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
        teamBlog.add(new Entry("first","My first blog entry."));
        teamBlog.add(new Entry("tutorial",
                "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));

        XStream xstream = new XStream();
        xstream.alias("blog", Blog.class);
        xstream.alias("entry", Entry.class);

        // 隐藏字段集合
        // xstream.addImplicitCollection(Blog.class, "entries");
        // 元素转为属性
        xstream.useAttributeFor(Blog.class, "writer");
        xstream.registerConverter(new AuthorConverter());
        // 先转化属性，后修改属性别名
        // xstream.aliasField("author", Blog.class, "writer");

        System.out.println(xstream.toXML(teamBlog));

    }

    public static void demo2(){
        Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
        teamBlog.add(new Entry("first","My first blog entry."));
        teamBlog.add(new Entry("tutorial", "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));

        XStream xstream = new XStream();
        xstream.alias("blog", Blog.class);
        xstream.alias("entry", Entry.class); // Blog
        // change the name of the writer tag
//        xstream.aliasField("author", Blog.class, "writer");

//          隐式集合：list的元素挨个显示不需要parent元素
        xstream.addImplicitCollection(Blog.class, "entries");

        System.out.println(xstream.toXML(teamBlog));
    }

    public static void demo1(){
        Cd bj = new Cd("basement_jaxx_singles");

        List order = new ArrayList();
// adds the same cd twice (two references to the same object)
        order.add(bj);
        order.add(bj);

// adds itself (cycle) 反向引用和交叉引用
        // Recursive reference to parent object
//        order.add(order);

        XStream xstream = new XStream();
        xstream.alias("cd", Cd.class);

        // XPath Relative mode
       // xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
        // Path absolute mode
       // xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

//        Single Node Selectors (absolute relative)
//        xstream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES);
//        xstream.setMode(XStream.SINGLE_NODE_XPATH_RELATIVE_REFERENCES);
//
//        更好读取
//        xstream.setMode(XStream.ID_REFERENCES);

//        no references 无引用
        // Recursive reference to parent object
//        order.add(order);
        xstream.setMode(XStream.NO_REFERENCES);

        System.out.println(xstream.toXML(order));
    }
    public static void main(String[] args) {
        // demo1();
        // demo2();
        demo3();
//        test();
//        demo6();
//         xmlToObject();

    }

}
